const { modelUser } = require('@/moudels/user.collection')

class UserService {
  // 用户注册
  async createUser(username, password, user_level) {
    const whereOpt = {username, password}
    user_level && Object.assign(whereOpt, {user_level})
    const res = await modelUser.create(
      [
        whereOpt
      ]
    )
    return res
  }
  // 查询用户
  async getUserInfo({id, username, password, is_admin}) {
    const whereOpt = {}

    id && Object.assign(whereOpt, {id})
    username && Object.assign(whereOpt, {username})
    password && Object.assign(whereOpt, {password})
    is_admin && Object.assign(whereOpt, {is_admin})

    if (Object.keys(whereOpt).length === 0) {
      return null; // 如果对象为空，返回null
    }
    const res = await modelUser.findOne(whereOpt)

    return res ? res : null
  }
}

module.exports = new UserService()